package com.hngtrust.iir.user.service.impl;

import com.hngtrust.framework.security.api.LoginAuthcationService;
import com.hngtrust.framework.security.entity.LoginUser;
import com.hngtrust.framework.security.exception.SecurityException;
import com.hngtrust.iir.exception.IirErrorType;
import com.hngtrust.iir.exception.IirException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/*******************************************************************************
 * Package: com.hngtrust.iir.user.service.impl
 * Type:    LoginAuthcationServiceImpl
 * Date:    2020-12-10 9:05
 *
 * Copyright (c) 2020 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/

/**
 * @author Chentianyong
 */
@Service
public class LoginAuthcationServiceImpl implements LoginAuthcationService {
    
    /**
     * Redis 模板
     */
    @Autowired
    private RedisTemplate redisTemplate;
    
    
    //OA验证URL地址
    @Value("${oa.aws.apiserver}")
    private String apiServer;
    
    //OA验证的cmd
    @Value("${oa.aws.cmd}")
    private String awsCmd;
    
    //OA验证key
    @Value("${oa.aws.accesskey}")
    private String accessKey;
    
    @Override
    public boolean loginAuthenticate(LoginUser loginUser, Map rawloginInfo) throws SecurityException {
        //如果系统自己的登录
        if(null != rawloginInfo.get("type") && rawloginInfo.get("type").toString().equals("1")){
            //图形验证
            if(null == rawloginInfo.get("reqTag")){
                throw new IirException(IirErrorType.VALIDATE_FAIL);
            }
            String pritureKey = "PVCODE_" + rawloginInfo.get("reqTag").toString();
            String pvcode = (String) redisTemplate.opsForValue().get(pritureKey);
            if(null == pvcode || !pvcode.equals(rawloginInfo.get("validateCode").toString().toUpperCase())){
                throw new IirException(IirErrorType.VALIDATE_FAIL);
            }
        }
        return true;
    }
}
